package com.lx.javasunmmrize.thread;

/**
 * @Description 线程同步实现, 解决并发问题
 * 1.同步方法
 * 2.同步代码块
 * 3.
 * @Author lx
 * @Date 2021/3/1 22:09
 **/
public class SynThread implements Runnable {

    //定义票
    private int tockit = 10;

    //1.同步方法
    @Override
    public synchronized void run() {
        while (true) {
            if (tockit <= 0) {
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + " 拿到了第" + tockit-- + "张票");
        }

    }

    public static void main(String[] args) {
        SynThread th = new SynThread();
        new Thread(th, "小明").start();
        new Thread(th, "校长").start();
        new Thread(th, "某某").start();
    }
}
